package com.hmkj.core.po.user;

import com.hmkj.common.baseMapper.GenericPo;
import com.hmkj.core.constant.PointNid;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

import javax.persistence.Table;
import java.util.Date;
import java.util.function.Consumer;


/**
 * 用户积分奖励记录表实体信息<br>
 *
 * @author eproo
 * @date 2017-11-24 03:05:38
 */
@Data
@EqualsAndHashCode(callSuper = true)
@ApiModel(description = "用户积分奖励记录表")
@Table(name = "biz_point_log")
public class PointLog extends GenericPo<Integer> {

    public static final String TABLE_NAME = "biz_point_log";


    /**
     * 用户ID
     **/
    @ApiModelProperty(value = "用户ID")
    private Integer userId;
    /**
     * 节点标识
     **/
    @ApiModelProperty(value = "奖励节点标识")
    private String nid;
    /**
     * 标题
     **/
    @ApiModelProperty(value = "标题")
    private String title;
    /**
     * 操作数量 增加为正减少为负
     **/
    @ApiModelProperty(value = "操作数量 增加为正减少为负")
    private Integer amount;
    /**
     * 操作后剩余积分数量
     **/
    @ApiModelProperty(value = "操作后剩余积分数量")
    private Integer totalLeft;
    /**
     * 备注
     **/
    @ApiModelProperty(value = "备注")
    private String remark;

    public PointLog() {
    }

    public PointLog(Consumer<PointLog> consumer) {
        consumer.accept(this);
    }

    public static PointLog buildLog(User user, int amount, PointNid nid, String remark) {
        return new PointLog(t -> {
            t.setTitle(nid.title);
            t.setNid(nid.nid);
            t.setUserId(user.getId());
            t.setAmount(amount);
            t.setRemark(remark);
            t.setCreateTime(new Date());
            t.setTotalLeft(user.getPoint() + amount < 0 ? 0 : user.getPoint() + amount);
        });
    }
}

